<?php
namespace Admin\Controller;



    public function add() {
        if(!IS_POST) {
            $this->display();
        } else {
            //echo $this->model->add($_POST) ? 'OK' : 'fail';

            // create()会把参数, 赋给内部的data,(如果没传参,则把$_POST赋给内部data)
            // create()的赋值过程,会检测数据的合法性, 自动验证将会发挥作用
            if(!$this->model->create()) {
                echo $this->model->getError();
            } else {
                if(isset($_FILES['goods_img'])) {
                    $up = new \Think\Upload();
                    $up->exts = array('jpeg' , 'jpg' , 'png' , 'gif');
                    $up->rootPath = APP_PATH;
                    $up->savePath = '/Public/images/';
                    
                    $info = $up->upload();
                    if($info) {
                        $this->model->ori_img = $info['goods_img']['savepath']  . $info['goods_img']['savename'];
                        $this->model->goods_img = $this->model->ori_img . '230X230.png';
                        $this->model->thumb_img = $this->model->ori_img . '100X100.png';
                    
                        // 缩略
                        $img = new \Think\Image();
                        $img->open(APP_PATH . $this->model->ori_img);

                        $img->thumb(230,230,\Think\Image::IMAGE_THUMB_FILLED);
                        $img->save(APP_PATH . $this->model->goods_img);

                        $img->thumb(100,100,\Think\Image::IMAGE_THUMB_FILLED);
                        $img->save(APP_PATH . $this->model->thumb_img);
                    }
                }
                // 上传图片
                echo $this->model->add() ? 'OK' : 'fail';
            }
        }
    }


}